package me.keiwu.admin.util;

import org.springframework.context.annotation.Bean;
import org.springframework.security.authentication.AnonymousAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;


public final class SecurityUtil {

    @Bean
    public static PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    public static String getCurrentUserUsername() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        String currentUserName = null;
        if (authentication != null && !(authentication instanceof AnonymousAuthenticationToken)) {
             currentUserName = authentication.getName();
        }
        return currentUserName;
    }

    /**
     * 加密密码
     */
    public static String encryptPassword(String password){
        return passwordEncoder().encode(password);
    }




}
